首先是预览图片,这个功能很好实现,无非就是创建一个html页面,嵌套一个<img>,为了限制图片类型,可以定义一个允许预览类型数组作为限制:
1 ///2 /// 预览图片 3 /// 4 /// 5 /// 6 ///7 public string PreviewPic(string physicalPath, string physicalDicPath) 8 { 9 string imageName = Path.GetFileNameWithoutExtension(physicalPath);10 string htmlName = imageName + ".html";11 if (!File.Exists(physicalDicPath + htmlName))12 {13 FileStream fs = new FileStream(physicalDicPath + htmlName, FileMode.CreateNew);14 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);15 StringBuilder sb = new StringBuilder();16 sb.Append(@"17 18 19 20 21 图片预览 22 36 ");37 sb.Append(@"");38 var TruePath = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalPath);39 sb.Append(@" ");40 sb.Append(@"");41 sb.Append(@"");42 sw.Write(sb.ToString()); //这里是写入的内容43 sw.Flush();44 sw.Close();45 }46 var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);47 return resultRul;48 }
然后就是预览excel文件,这个微软为我们提供了现成的方法,打开nuget管理,安装Microsoft.Office.Interop.Excel;经测试xls和xlsx格式都可以成功解析,然后代码如下:
1 ///2 /// 预览Excel 3 /// 4 /// 文件物理路径 5 /// 文件夹物理路径 6 ///生成页面链接 7 public string PreviewExcel(string physicalPath, string physicalDicPath) 8 { 9 string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";10 if (!File.Exists(physicalDicPath + htmlName))11 {12 Microsoft.Office.Interop.Excel.Application application = null;13 Microsoft.Office.Interop.Excel.Workbook workbook = null;14 application = new Microsoft.Office.Interop.Excel.Application();15 object missing = Type.Missing;16 object trueObject = true;17 application.Visible = false;18 application.DisplayAlerts = false;19 workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,20 missing, missing, missing, missing, missing, missing, missing, missing, missing);21 //Save Excel to Html22 object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;23 String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;24 workbook.SaveAs(outputFile, format, missing, missing, missing,25 missing, XlSaveAsAccessMode.xlNoChange, missing,26 missing, missing, missing, missing);27 workbook.Close();28 application.Quit();29 }30 var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);31 return resultRul;32 }
最后就是word的预览了,word的话有两个常见格式:doc,docx;在测试该方法的时候,打开doc格式的word,每次都会弹出转换格式的弹窗,上网查了一下,原来的
Documents.Open()
方法的第二个参数是:真正显示转换文件对话框,如果该文件不是Microsoft Word格式。所以我们只需要将 ConfirmConversions设置为false即可规避这个问题;代码如下:
1 ///2 /// 预览Excel 3 /// 4 /// 文件格式 5 /// 文件物理路径 6 /// 文件夹物理路径 7 ///生成页面链接 8 public string PreviewWord(string physicalPath, string physicalDicPath) 9 {10 string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";11 if (!File.Exists(physicalDicPath + htmlName))12 {13 Microsoft.Office.Interop.Word._Application application = null;14 Microsoft.Office.Interop.Word._Document doc = null;15 application = new Microsoft.Office.Interop.Word.Application();16 object missing = Type.Missing;17 object trueObject = true;18 object falseObject = false;19 application.Visible = false;20 application.DisplayAlerts = WdAlertLevel.wdAlertsNone;21 doc = application.Documents.Open(physicalPath, falseObject, trueObject, missing, missing, missing,22 missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);23 object format = format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;24 String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;25 doc.SaveAs(outputFile, format, missing, missing, missing,26 missing, missing, missing,27 missing, missing, missing, missing);28 doc.Close();29 application.Quit();30 }31 var resultRul= "http://"+HttpContext.Current.Request.Url.Authority +"/"+ urlconvertor(physicalDicPath + htmlName);32 return resultRul;33 }
最后是预览text,这个实现思想和预览图片是一样的,读取文档内容,填充到html页面上,代码如下:
1 ///2 /// 预览Txt 3 /// 4 /// 文件物理路径 5 /// 文件夹物理路径 6 ///生成页面链接 7 public string PreviewTxt(string physicalPath, string physicalDicPath) 8 { 9 FileStream aFile = new FileStream(physicalPath, FileMode.Open);10 //暂时不知道为什么获取的编码方式会导致读取的内容为空11 //Encoding codeType = GetType(aFile);12 StreamReader sr = new StreamReader(aFile, Encoding.GetEncoding("GB2312"));13 var content= sr.ReadToEndAsync().Result.Replace("\r\n","");14 string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";15 if (!File.Exists(physicalDicPath + htmlName))16 {17 FileStream fs = new FileStream(physicalDicPath + htmlName, FileMode.CreateNew);18 StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);19 StringBuilder sb = new StringBuilder();20 sb.Append(@"21 22 23 24 25文本预览 26 36 ");37 sb.Append(@"");38 sb.Append(@"");41 sb.Append(@"");42 sw.Write(sb.ToString()); //这里是写入的内容43 sw.Flush();44 sw.Close();45 }46 sr.Close();47 var resultRul = "http://" + HttpContext.Current.Request.Url.Authority + "/" + urlconvertor(physicalDicPath + htmlName);48 return resultRul;49 }");39 sb.Append(content);40 sb.Append(@"